package co.fitstart.admin.entity.content;

import java.util.List;
import java.util.Map;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.round.RoundStatus;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author mohanlan
 *
 */
public class RoundSearch implements QueryBuilder {
    
    private String name;
    
    private String mobile;
    
    private Long coach;
    
    private Integer group;
    
    private String dishonesty;
    
    private List<CourseRound> dishonestyRound;
    
    private String status;
    
    private String courseType;
    
    private boolean experience;
    
    private boolean give;
    
    private Long site;
    
    private List<Site> sites;
    
    private Order order;
    
    private Filter filter;
    
    /**
     * default constructor
     */
    public RoundSearch() {
        order = new Order("createTime", "DESC");
    }

    /*f
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {

        if(filter == null || refresh) {
            filter = new Filter().addSentence("courseRound.site IN (:sites)", "sites", sites)
                                 .addEquals("courseRound.site.id", site)
                                 .addEquals("courseRound.courseType", CourseType.aliasOf(courseType))
                                 .addEquals("courseRound.courseStatus", RoundStatus.aliasOf(status))
                                 .addLike("courseRound.student.name", name)
                                 .addEquals("courseRound.student.mobile", mobile)
                                 .addEquals("courseRound.course.experience", experience)
                                 .addEquals("courseRound.course.give", give)
                                 .addEquals("courseRound.coach.id", coach);
            if("1".equals(dishonesty)) {
                filter.addSentence("courseRound in ( :dishonestyRound)", "dishonestyRound", dishonestyRound);
            } else if("0".equals(dishonesty)) {
                if(dishonestyRound != null && dishonestyRound.size() != 0) {
                    filter.addSentence("courseRound not in ( :dishonestyRound)", "dishonestyRound", dishonestyRound);
                }
            }
            
            if(group != null) {
                if(group == 0) {
                    filter.addIsNull("courseRound.fitnessGroup");
                } else {
                    filter.addIsNotNull("courseRound.fitnessGroup");
                }
            }
            
        }

        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM CourseRound courseRound WHERE courseRound.disabled = false " + getPreparedFilter(false).getClauseSentence() + order.getSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the coach
     */
    public Long getCoach() {
        return coach;
    }

    /**
     * @return the group
     */
    public Integer getGroup() {
        return group;
    }

    /**
	 * @return the dishonesty
	 */
	public String getDishonesty() {
		return dishonesty;
	}

	/**
	 * @return the dishonestyRound
	 */
	public List<CourseRound> getDishonestyRound() {
		return dishonestyRound;
	}

	/**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @return the courseType
     */
    public String getCourseType() {
        return courseType;
    }

    /**
     * @return the experience
     */
    public boolean isExperience() {
        return experience;
    }

    public boolean isGive() {
        return give;
    }

    /**
     * @return the site
     */
    public Long getSite() {
        return site;
    }

    /**
     * @return the sites
     */
    public List<Site> getSites() {
        return sites;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param coach the coach to set
     */
    public void setCoach(Long coach) {
        this.coach = coach;
    }

    /**
     * @param group the group to set
     */
    public void setGroup(Integer group) {
        this.group = group;
    }

    /**
	 * @param dishonesty the dishonesty to set
	 */
	public void setDishonesty(String dishonesty) {
		this.dishonesty = dishonesty;
	}

	/**
	 * @param dishonestyRound the dishonestyRound to set
	 */
	public void setDishonestyRound(List<CourseRound> dishonestyRound) {
		this.dishonestyRound = dishonestyRound;
	}

	/**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @param courseType the courseType to set
     */
    public void setCourseType(String courseType) {
        this.courseType = courseType;
    }

    /**
     * @param experience the experience to set
     */
    public void setExperience(boolean experience) {
        this.experience = experience;
    }

    /**
     * 
     * @param give the give to set
     */
    public void setGive(boolean give) {
        this.give = give;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Long site) {
        this.site = site;
    }

    /**
     * @param sites the sites to set
     */
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }
}
